//
//  SLTextTableHeaderView.m
//  Objective-C
//
//  Created by 陆承东 on 2019/1/23.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLTextTableHeaderView.h"

#import "UIView+YYAdd.h"
#import "UIImageView+YYWebImage.h"

@interface SLTextTableHeaderView()

@property (nonatomic, weak) UIImageView *bannerImageView;

@property (nonatomic, weak) UIView *bottomBackView;
@property (nonatomic, weak) UIImageView *iconImageView;
@property (nonatomic, weak) UIView *textBackView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *subTitleLabel;

@end

@implementation SLTextTableHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    [self removeAllSubviews];
    
    UIImageView *bannerImageView = [[UIImageView alloc] init];
    self.bannerImageView = bannerImageView;
    [self addSubview:bannerImageView];
    
    UIView *bottomBackView = [[UIView alloc] init];
    self.bottomBackView = bottomBackView;
    [self addSubview:bottomBackView];
    
    UIImageView *iconImageView = [[UIImageView alloc] init];
    self.iconImageView = iconImageView;
    [self addSubview:iconImageView];
    
    UIView *textBackView = [[UIView alloc] init];
    self.textBackView = textBackView;
    [self addSubview:textBackView];
    
    UILabel *titleLabel = [self labelWithFont:[UIFont systemFont14] textColor:[UIColor blackColor] text:@""];
    self.titleLabel = titleLabel;
    [textBackView addSubview:titleLabel];
    
    UILabel *subTitleLabel = [self labelWithFont:[UIFont systemFont11] textColor:[UIColor darkGrayColor] text:@""];
    self.subTitleLabel = subTitleLabel;
    [textBackView addSubview:subTitleLabel];
}

- (void)setData {
    CGFloat bannerImageViewH = kScreenWidth / 750.0 * 350.0;
    CGFloat bottomBackViewH = 90.0;
    
    [self.bannerImageView setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1548240745088&di=bd27904a3e19ff7eb23900c036d66ccc&imgtype=0&src=http%3A%2F%2Fpic.90sjimg.com%2Fback_pic%2Fqk%2Fback_origin_pic%2F00%2F01%2F35%2F0e4a19bb49fd227c3bb1f15cbe559ec1.jpg"] options:YYWebImageOptionIgnoreDiskCache];
    [self.bannerImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(bannerImageViewH);
    }];
    
    [self.bottomBackView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bannerImageView.mas_bottom).mas_offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(bottomBackViewH);
    }];
    
    [self.iconImageView setImageWithURL:[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1548242833851&di=aa9587c37cc5291c3d31531eed169d14&imgtype=0&src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F01%2F61%2F79%2F905748de7568302.jpg"] options:YYWebImageOptionIgnoreDiskCache];
    [self.iconImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.width.height.mas_equalTo(50);
        make.centerY.equalTo(self.bottomBackView.mas_centerY).mas_offset(0);
    }];
    
    [self.textBackView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).mas_offset(8);
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(self.bottomBackView.mas_centerY).mas_offset(0);
    }];
    
    self.titleLabel.text = @"Title";
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    
    self.subTitleLabel.text = @"Subtitle";
    [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(8);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(bannerImageViewH + bottomBackViewH);
    }];
}

- (UILabel *)labelWithFont:(UIFont *)font textColor:(UIColor *)textColor text:(NSString *)text {
    UILabel *label = [UILabel new];
    label.font = font;
    label.textColor = textColor;
    label.text = text;
    return label;
}

@end
